//
//  Rx+do.swift
//  SwiftCode
//
//  Created by yuhus on 2023/12/14.
//

import RxSwift

func doRx() {
  doError()
  doNext()
}

private func doError() {
  _ = Observable<Void>.error(AccountError.loginFailed)
    .do(onNext: { _ in
      Timber.d(#function, "do#onNext")
    }, afterNext: { _ in
      Timber.d(#function, "do#afterNext")
    }, onError: { error in
      Timber.d(#function, "do#onError")
    })
    .subscribe(onNext: { _ in
      Timber.d(#function, "subscribe#onNext")
    } ,onError: { _ in
      Timber.d(#function, "subscribe#onError")
    })
  
  _ = Observable<Void>.error(AccountError.loginFailed)
    .do { _ in
      Timber.d(#function, "do#default")
    }
    .subscribe {_ in
      Timber.d(#function, "subscribe#default")
    }
}

private func doNext() {
  _ = Observable.just(1)
    .do(onNext: {
      Timber.d(#function, "do#onNext", $0)
    }, afterNext: {
      Timber.d(#function, "do#afterNext", $0)
    })
    .subscribe(onNext: {
      Timber.d(#function, "subscribe#onNext", $0)
    })
  
  _ = Single.just(2)
    .do {
      Timber.d(#function, "do", $0)
    }
    .subscribe {
      Timber.d(#function, "subscribe", $0)
    }
}
